Shader "Unlit/AnimInstancing"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
        _AnimTex ("AnimTex", 2D) = "white" {}
        _AnimTime ("Time",Range(0.0,1.0))=0.0
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 100

        Pass
        {
            CGPROGRAM
            #pragma target 3.5
            #pragma vertex vert
            #pragma fragment frag
            #pragma multi_compile_instancing

            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
                uint vid: SV_VERTEXID;
                UNITY_VERTEX_INPUT_INSTANCE_ID
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                float4 vertex : SV_POSITION;
                UNITY_VERTEX_INPUT_INSTANCE_ID
            };

            sampler2D _MainTex;
            sampler2D _AnimTex;
            UNITY_INSTANCING_BUFFER_START(Props)
                UNITY_DEFINE_INSTANCED_PROP(float4,_AnimTex_TexelSize)
                UNITY_DEFINE_INSTANCED_PROP(float4,_MainTex_ST)
                UNITY_DEFINE_INSTANCED_PROP(float,_AnimTime)
            UNITY_INSTANCING_BUFFER_END(Props)
            

            v2f vert (appdata v)
            {
                v2f o;
                UNITY_SETUP_INSTANCE_ID(v);
                UNITY_TRANSFER_INSTANCE_ID(v,o);
                //float4 uv_anim = float4( (v.vid + 0.5) * UNITY_ACCESS_INSTANCED_PROP(Props,_AnimTex_TexelSize).x, _Time.y/UNITY_ACCESS_INSTANCED_PROP(Props,_AnimTime), 0, 0);
                float4 uv_anim = float4( (v.vid + 0.5) * UNITY_ACCESS_INSTANCED_PROP(Props,_AnimTex_TexelSize).x, UNITY_ACCESS_INSTANCED_PROP(Props,_AnimTime) * _Time.y, 0, 0);
                v.vertex = float4(tex2Dlod(_AnimTex,uv_anim).xyz,1.0);
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = v.uv * UNITY_ACCESS_INSTANCED_PROP(Props,_MainTex_ST).xy+UNITY_ACCESS_INSTANCED_PROP(Props,_MainTex_ST).zw;
                return o;
            }

            fixed4 frag (v2f i) : SV_Target
            {
                // sample the texture
                fixed4 col = tex2D(_MainTex, i.uv);
                return col;
            }
            ENDCG
        }
    }
}
